package com.yfbao.horizon.user.injection.algorithm;

import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class MyHintShardingAlgorithm implements HintShardingAlgorithm<Integer> {

    @Override
    public Collection<String> doSharding(Collection<String> collection, HintShardingValue<Integer> hintShardingValue) {

        String logicTableName = hintShardingValue.getLogicTableName();
        Collection<Integer> values = hintShardingValue.getValues();
        List<String> tableList = new ArrayList<>();

        if(values ==null || values.isEmpty()){
            return collection;
        }
        for (Integer value : values) {
            String tableName = logicTableName+"_"+value%3;
            tableList.add(tableName);
        }

        return tableList;
    }
}
